var radomT = "?_=" + (new Date()).getTime();
document.domain = "10010.com";

//zhaohailong start 2015/02/10
//记录loadding蒙层标示 
var flag = "0";
//记录个别业务加载loadding蒙层(信息总览、升级4G组合套餐、3G流量包)
var recordFlag = undefined;
//加载次数记录   当第一次加载完成后，记录reqFlag为1，当第二次加载时，这时候记录reqFlag为2
var commonReqFlag = "1";
//记录加载次数        当第一次加载完成后，记录reqFlag为1，当第二次加载时，这时候记录reqFlag为2   zhaohailong 2015/02/10
var headerReqFlag = "1";
//zhaohailong end

var wt2BaseCommon = {
	loginSuccess : function() {
		$('#login_iframe').removeAttr('src');
		CommonMain.loadData("/l",null,'Login.on(data)');
	}
};
var mallWebBaseCommon = {
	loginSuccess : function() {
		$('#login_iframe').removeAttr('src');
		CommonMain.loadData("/l",null,'Login.on(data)');
	}
};
String.prototype.endWith=function(s){
	if(s==null||s==""||this.length==0||s.length>this.length)
		return false;
	if(this.substring(this.length-s.length)==s)
		return true;
	else
		return false;
	return true;
};
var Login={
	on:function(data){
		if(data.result){
			//如果是邮箱登录，未绑定手机号码
			if(data.binding){
				$('#login_footer').hide();
				_BaseTools.showid("bondingArry");
				//window.location.href= E3CommonsVariables.BindingPage();
			}else{
				if(window.location.href.indexOf('/e3/index.html') !=-1 || window.location.href.endWith('/e3/')){
					if("12" ==data.nettype|| "13" ==data.nettype|| "14" ==data.nettype|| "03" ==data.nettype|| "04" ==data.nettype|| "05" ==data.nettype){
						window.location.href='/e3/query/personal_xx.html';
					}else{
						window.location.href='/e3/index_server.html';
					}
				}else{
					$('#login_footer').hide();
					$('div#layer').remove();
					window.location.reload(true);
				}
			}
		} else {
			var message = "登录失败，请稍后再试。";
			_BaseTools.setErrorMessage("", message);
			_BaseTools.showid('wrongTc');
		}
	}
};

/**
 * 获取URL请求参数
 */
var GetParam = {
	getURLParameter : function(name) {
		return decodeURIComponent((new RegExp('[?|&]' + name + '='
				+ '([^&;]+?)(&|#|;|$)').exec(window.location.search) || [ , "" ])[1]
				.replace(/\+/g, '%20'))
				|| null;
	},
	getParamsByMonth : function() {
		return GetParam.getURLParameter("monthDate");
	},getUrlNoParameter:function(){
		var thisurl =window.location.href;
		var indexOfUrlAfter=thisurl.indexOf("?");
		var partUrl="";
		if(indexOfUrlAfter==-1){
			partUrl=thisurl;
		}else
		{
			partUrl= thisurl.substring(0,indexOfUrlAfter);
		}
		return partUrl;
	}
};

/**
 * This is Commons Variables For e3
 * 
 * Can be expanded as needed
 */
var E3CommonsVariables = {
	// Unified request prefix
	RequestPrefix : function() {
		return "/e3/static";
	},
	RequestPrefixCommon : function() {
		return "/e3/static/common";
	},
	RequestPrefixQuery : function() {
		return "/e3/static/query";
	},
	RequestPrefixTran : function() {
		return "/e3/static/transact";
	},
	RequestPrefixLife : function() {
		return "/e3/static/life";
	},
	BindingPage:function(){
		return "/e3/query/binding_number.html";
	}
};
var theBaseMain={
		loadData : function(argUrl, paramdata, fun) {
			if(!_BaseTools.isLogin()){
				return;
			}
			var url = E3CommonsVariables.RequestPrefix() + argUrl + "?_=" + (new Date()).getTime();
			jQuery.ajax({
				url : url,
				type : "POST",
				data : paramdata,
				async : true,
				dataType : "JSON",
				contentType : "application/x-www-form-urlencoded;charset=UTF-8",
				complete : closeMask,
				success : function(data) {
					if (null != data && "" != data) {
						
//						if (null != data.versionValidate) {
//							if (data.versionValidate.forward) {
//								window.location.href=data.versionValidate.url;
//								return;
//							}
//						}
						if (null != data.acclimit) {
							if ("true"==data.acclimit) {
								window.location.href="/e3/query/baocuo3.html";
								return;
							}
						}
						if( data.ipValidate != null ){
							window.location.href="/e3/ipbaocuo.html";
							return;
						}
					}
					eval(fun);
				}
			});
		},
		// 处理数据，加载jstemplate模板
		processTemplateData : function(show, templateId, data) {
			$(show).setTemplateElement(templateId).processTemplate(data);
		}
};
var searchMain={
		loadData : function(argUrl, paramdata, fun) {
			var url = E3CommonsVariables.RequestPrefix() + argUrl + "?_=" + (new Date()).getTime();
			if (argUrl=="/chinaunicomSearchJB/search/api.do"){     //对服务类搜索的请求url特别处理
			url = argUrl + "?_=" + (new Date()).getTime();
			}			
			jQuery.ajax({
				url : url,
				type : "POST",
				data : paramdata,
				async : true,
				dataType : "JSON",
				contentType : "application/x-www-form-urlencoded;charset=UTF-8",
				complete : closeMask,
				success : function(data) {
					if (null != data.acclimit) {
						if ("true"==data.acclimit) {
							window.location.href="/e3/query/baocuo3.html";
							return;
						}
					}
					if (null != data && "" != data) {
						if( data.ipValidate != null ){
							window.location.href="/e3/ipbaocuo.html";
							return;
						}
					}
					eval(fun);
				},
				error:function(){
					$("#queryError").show();
					var obj =new Object();
					obj.errorMessage=new Object();
					obj.errorMessage.respCode="";
					obj.errorMessage.respDesc="尊敬的客户，由于同一时间使用该业务人数较多，暂时无法为您提供服务，为您带来的不便敬请谅解。";
					_BaseTools.queryErrorMessageObj("queryError",obj);
				}
			});
		},
		// 处理数据，加载jstemplate模板
		processTemplateData : function(show, templateId, data) {
			$(show).setTemplateElement(templateId,null, {filter_data : false}).processTemplate(data);
		}
};
var CommonMain = {
	loadData : function(argUrl, paramdata, fun) {
		var menuid = jQuery("#menu_right_top").find("li[class='on']").children(
				"a").attr("id");
		radomT = "?_=" + (new Date()).getTime();
		var url = E3CommonsVariables.RequestPrefixCommon() + argUrl + radomT;
		if (menuid != undefined) {
			url += "&menuid=" + menuid;
		}
		jQuery.ajax({
			url : url,
			type : "POST",
			data : paramdata,
			async : true,
			dataType : "JSON",
			contentType : "application/x-www-form-urlencoded;charset=UTF-8",
			complete : closeMask,
			success : function(data) {
				if (null != data && "" != data) {
				/*	if (null != data.versionValidate) {
						if (data.versionValidate.forward) {
							window.location.href=data.versionValidate.url;
							return;
						}
					}*/
					if (null != data.acclimit) {
						if ("true"==data.acclimit) {
							window.location.href="/e3/query/baocuo3.html";
							return;
						}
					}
					if( data.ipValidate != null ){
						window.location.href="/e3/ipbaocuo.html";
						return;
					}
				}
				eval(fun);
			}
		});
	},
	// 处理数据，加载jstemplate模板
	processTemplateData : function(show, templateId, data) {
		$(show).setTemplateElement(templateId).processTemplate(data);
	}
};
var QueryMain = {
	loadData : function(argUrl, paramdata, fun) {
		if(!_BaseTools.isLogin()){
			return;
		}
		var menuid = _BaseTools.getNaviId();
		/*var menuid = jQuery("#menu_right_top").find("li[class='on']").children(
				"a").attr("id");*/
		radomT = "?_=" + (new Date()).getTime();
		var url = E3CommonsVariables.RequestPrefixQuery() + argUrl + radomT;
		if (menuid != undefined) {
			url += "&menuid=" + menuid;
		}
		jQuery.ajax({
			url : url,
			type : "POST",
			data : paramdata,
			async : true,
			dataType : "JSON",
			contentType : "application/x-www-form-urlencoded;charset=UTF-8",
			//complete : closeMask,
			complete:function(){
				$("#center_loadingBg").hide();
				$("#center_loadingGif").hide();
			},
			beforeSend : function() {
				// 可加载loding框
				$("#center_loadingBg").show();
				$("#center_loadingGif").show();
			},
			success : function(data) {
				if (null != data && "" != data) {
				/*	if (null != data.versionValidate) {
						if (data.versionValidate.forward) {
							window.location.href=data.versionValidate.url;
							return;
						}
					}*/
					if(data.limited == "00"){
						$(".wapContent_r").setTemplateElement("queryErrorTemplate_menuCheck").processTemplate(data);
						$('#menuCheck_limitedmessage').html("尊敬的用户，您当前省份暂时未开放此业务，您可关注其他业务");
						return;
					}
					if(data.limited == "01"){
						$(".wapContent_r").setTemplateElement("queryErrorTemplate_menuCheck").processTemplate(data);
						$('#menuCheck_limitedmessage').html("尊敬的用户，您目前采用随机码登录，此业务暂时无法提供服务，请使用服务密码登录后再使用此业务。");
						return;
					}
					if (null != data.acclimit) {
						if ("true"==data.acclimit) {
							window.location.href="/e3/query/baocuo3.html";
							return;
						}
					}
					if( data.ipValidate != null ){
						window.location.href="/e3/ipbaocuo.html";
						return;
					}
				}
				eval(fun);
				
			},
			error:function(){
				//报错页面
				var ErrorOject=new Object();
				ErrorOject.errorCode="";
				ErrorOject.errorDesc="很抱歉，网络异常了，请查看是否需要您重新登录或者稍后再试。";
				QueryMain.processTemplateData("#queryError","queryErrorTemplate",ErrorOject);

			}
		});
	},
	// 处理数据，加载jstemplate模板
	processTemplateData : function(show, templateId, data) {
		$(show).setTemplateElement(templateId).processTemplate(data);
	}
};
var TransactMain = {
	loadData : function(argUrl, paramdata, fun) {
		var menuid = _BaseTools.getNaviId();
		radomT = "?_=" + (new Date()).getTime();
		var url = E3CommonsVariables.RequestPrefixTran() + argUrl + radomT;
		if (menuid != undefined) {
			url += "&menuid=" + menuid;
		}
		jQuery.ajax({
			url : url,
			type : "POST",
			data : paramdata,
			async : true,
			dataType : "JSON",
			contentType : "application/x-www-form-urlencoded;charset=UTF-8",
			complete:function(){
				$("#center_loadingBg").hide();
				$("#center_loadingGif").hide();
			},
			beforeSend : function() {
				// 可加载loding框
				$("#center_loadingBg").show();
				$("#center_loadingGif").show();
				setTimeout("$('#center_loadingBg,#center_loadingGif').hide();", 8000);
			},
			success : function(data) {
				if (null != data && "" != data) {
					/*if (null != data.versionValidate) {
						if (data.versionValidate.forward) {
							window.location.href=data.versionValidate.url;
							return;
						}
					}*/
					if(data.limited == "00"){
						$(".wapContent_r").setTemplateElement("queryErrorTemplate_menuCheck").processTemplate(data);
						$('#menuCheck_limitedmessage').html("尊敬的用户，您当前省份暂时未开放此业务，您可关注其他业务");
						return;
					}
					if(data.limited == "01"){
						$(".wapContent_r").setTemplateElement("queryErrorTemplate_menuCheck").processTemplate(data);
						$('#menuCheck_limitedmessage').html("尊敬的用户，您目前采用随机码登录，此业务暂时无法提供服务，请使用服务密码登录后再使用此业务。");
						return;
					}
					if (null != data.acclimit) {
						if ("true"==data.acclimit) {
							window.location.href="/e3/query/baocuo3.html";
							return;
						}
					}
					if( data.ipValidate != null ){
						window.location.href="/e3/ipbaocuo.html";
						return;
					}
				}
				eval(fun);

			}
		});
	}
};

var LifeMain = {
	loadData : function(argUrl, paramdata, fun,errorfun) {
		var menuid = jQuery("#menu_right_top").find("li[class='on']").children(
				"a").attr("id");
		radomT = "?_=" + (new Date()).getTime();
		var url = E3CommonsVariables.RequestPrefixLife() + argUrl + radomT;
		if (menuid != undefined) {
			url += "&menuid=" + menuid;
		}
		jQuery.ajax({
			url : url,
			type : "POST",
			data : paramdata,
			async : true,
			dataType : "JSON",
			contentType : "application/x-www-form-urlencoded;charset=UTF-8",
			complete:function(){
				$("#center_loadingBg").hide();
				$("#center_loadingGif").hide();
			},
			beforeSend : function() {
				// 可加载loding框
				$("#center_loadingBg").show();
				$("#center_loadingGif").show();
			},
			success : function(data) {
				if (data && null != data.acclimit) {
					if ("true"==data.acclimit) {
						window.location.href="/e3/query/baocuo3.html";
						return;
					}
				}
				if(data&& data.ipValidate != null ){
					window.location.href="/e3/ipbaocuo.html";
					return;
				}
				eval(fun);
			},
			error:function(){
				//公共报错页面
				if(errorfun!=null)
				{
					eval(errorfun);
				}
				$("#queryError").show();
				_BaseTools.queryErrorMessageObj("queryError","尊敬的客户，由于同一时间使用该业务人数较多，暂时无法为您提供服务，为您带来的不便敬请谅解。");
			}
		});
	}
};

function logout(){
	CommonMain.loadData("/logout", null, 'logout_ballback()');
}
function logout_ballback(){
	if(window.location.href.indexOf("luckDraw/index.html")!=-1){
		window.location.reload();
	}else{
		$.cookie('JUT', null, { path: '/' });
		$.cookie('MIE', null, { path: '/' });
		$.cookie('MII', null, { path: '/' });
		window.location='http://iservice.10010.com/e3/index.html';
	}
}

$(document).ready(function(){
	if($.cookie('JUT')!=null){
		  $('#header2islogin').html('欢迎来到中国联通！<a href="javascript:;" onclick="logout()">[退出]</a>');
	}else{
		  var _log_html = '欢迎来到中国联通！<a href="javascript:;">[登录]</a> <a href="javascript:;" style=" color:#ff6600;">[免费注册]</a>';
		  $('#header2islogin').html(_log_html);
	}
	
});

//手机密令
window.parent.wt2BaseCommon.secureAuth = function(secure_token,secure_tool,user_number) {
	jQuery.ajax({
		url:E3CommonsVariables.RequestPrefixQuery()+"/checkmapExtraParamResult?_=" + (new Date()).getTime(),
		type:"POST", 
		data:{
			secure_token:secure_token,
			secure_tool:secure_tool,
			user_number:user_number
		},
		dataType:"JSON",
		success:function(data){
			if(data){
				var cookie_usermail = jQuery.cookie('usermiliauth');
				if(null==cookie_usermail||""==cookie_usermail){
					cookie_usermail=usermiliauth;
				}else{
					cookie_usermail+=usermiliauth;
				}
				jQuery.cookie('usermiliauth', cookie_usermail, {domain: '.10010.com'});
				usermiliauth="";
				OverScreenPass(0);
				window.location.reload();
			}else{
				alert('尊敬的用户您好:系统忙请稍后再试');
				$("#secureIframe").attr("src","");
			}
		}
	});
}
window.parent.wt2BaseCommon.closeMiliAuth = function() {
	//jQuery.cookie('usermiliauth', 'usermiliauth', {domain: '.10010.com'});
	OverScreenPass(0);
}

function checkmapExtraParam(menuId,fun,v,s,t){
	var check_bol = false;
	jQuery.ajax({
		url:E3CommonsVariables.RequestPrefixQuery()+"/checkmapExtraParam?_=" + (new Date()).getTime(),
		type:"POST", 
		data:{
			menuId:menuId
		},
		async:false,
		dataType:"JSON",
		success:function(data){
			if(data.morder){
		    	var cookie_usermail = jQuery.cookie('usermiliauth');
		    	if(null==cookie_usermail||""==cookie_usermail){
		    		var secureIframe="http://uac.10010.com/secure/securecenter/ylsc"+data.morderCode+"yzche/securelayer?redirect_url=http://iservice.10010.com";
				    jQuery("#secureIframe").attr("src",secureIframe);
		    		showpassover();
			     	usermiliauth=data.miliauth+"$";
		    	}else{
		    		var usermil = cookie_usermail.split("$");
			    	for (var i=0;i<usermil.length;i++) {
			    		if(usermil[i]==data.miliauth){
			    			fun(v,s,t);
			    			check_bol = true;
			    		}
					}
			    	if(!check_bol){
			    		var secureIframe="http://uac.10010.com/secure/securecenter/ylsc"+data.morderCode+"yzche/securelayer?redirect_url=http://iservice.10010.com";
					    jQuery("#secureIframe").attr("src",secureIframe);
			    		showpassover();
				     	usermiliauth=data.miliauth+"$";
			    	}
		    	}
		     }else{
		     	usermiliauth="";
		     	fun(v,s,t);
		     	check_bol = true;
		     }
		}
	});
	return check_bol;
}
var CoverLayerID = 'CoverLayer';
function showpassover(){
	/*if((jQuery.cookie('usermiliauthMorder')==null||jQuery.cookie('usermiliauthMorder')=="")&&(jQuery.cookie('usermiliauth')==""||jQuery.cookie('usermiliauth')==null)){
		jQuery('#password_layer_id').show();
		OverScreenPass(1);
		return false;
	}
	return true;*/
	window.document.domain='10010.com';
	jQuery('#password_layer_id').show();
	OverScreenPass(1);
	return false;
}
function OverScreenPass(tag){
	if(jQuery('#'+CoverLayerID).length>0){
	  with(jQuery('#'+CoverLayerID)){
		  if(tag==1){
			  LayoutAttr(jQuery('#'+CoverLayerID));
		  }else{
		  	  jQuery('#password_layer_id').hide();
		  	  jQuery('.closeT').removeAttr('click');
			  css('display','none');
		  }
	  }
	}else if(tag==1){
		var CoverLayer = jQuery('<div></div>');
		CoverLayer.appendTo('body');
		with(CoverLayer){
			attr('id',CoverLayerID);
			LayoutAttr(CoverLayer);
		}
	}
}
function LayoutAttr(obj){
	with(obj){
		css('position','absolute');
		css('min-width','100%');
		css('background-color','#CCC');
		css('z-index','99988');
		css('width',window.screen.width-30);
		css('left','0px');
		css('top','0px');
		css('display','block');
		css('opacity',0.8);
		css('height',jQuery(document).height());
	}
}

//关闭蒙层      zhaohailong 2015/02/10
function closeMask(XMLHttpRequest, textStatus){
	//当flag为0时，说明用户信息还未加载，这时候记录flag为1，说明请求业务数据逻辑层已加载完毕，记录flag为1，
	if (flag == "0") {
		flag = "1";
		//记录reqFlag为2时，说明已经加载过业务数据逻辑层了
		commonReqFlag = "2";//第一次请求
	} else {
		//当reqFlag为2时，说明已经加载过了，这时候只需要还原成1就可以
		if (commonReqFlag == "2") {
			if (headerReqFlag == "2") {
				if (recordFlag) {
					setTimeout("$('#loadingGif,#loadingBg').hide();",1000);
					recordFlag = undefined;
					commonReqFlag = "1";
				} 
			} else {
				commonReqFlag = "3";
			}
		} else {
			commonReqFlag = "2";
			if (!recordFlag) {
				setTimeout("$('#loadingGif,#loadingBg').hide();",1000);
			}
			//当第一次请求时，并且flag为1时，说明用户信息已加载完毕，关闭蒙层就可以
		}
	}
}
